package com.yanggu.jt808.web;

import com.yanggu.jt808.config.ChannelManager;
import com.yanggu.jt808.consts.Jt808Constant;
import com.yanggu.jt808.model.base.DataPackage;
import com.yanggu.jt808.model.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * TerminalController
 * 终端控制
 *
 * @author
 * @date 2020/5/26 15:14
 */
@RestController
@RequestMapping(value = "/api/terminal")
public class TerminalController {

    @Autowired
    private ChannelManager channelManager;

    // @PostMapping(value = "/position")
    // public ResultVO findPosition(@RequestParam("terminalId") String terminalId) {
    //
    //     DataPacket dataPacket = new DataPacket();
    //     dataPacket.getHeader().setMsgId(JT808Const.SERVER_MSG_FIND_LOCATION);
    //     dataPacket.getHeader().setTerminalPhone(terminalId);
    //
    //     ChannelPromise promise = positionHandler.sendMessage(dataPacket);
    //     try {
    //         promise.await(3, TimeUnit.SECONDS);
    //         PositionReply response = positionHandler.getResponse();
    //
    //         return new ResultVO(CODE_SUCCESS, "成功！", response);
    //     } catch (InterruptedException e) {
    //         e.printStackTrace();
    //         return new ResultVO(CODE_FAIL, "失败！", e);
    //     }
    // }


    @GetMapping(value = "/findParameters")
    public ResultVO findParameters(@RequestParam("terminalId") String terminalId) {

        DataPackage dataPacket = new DataPackage();
        dataPacket.getHeader().setMsgId(Jt808Constant.查询终端参数);
        dataPacket.getHeader().setTerminalPhone(terminalId);

        // bytes 是不包含校验码以及转义的数据 因为发送之前会自动添加校验码以及转义数据

        channelManager.get(terminalId).writeAndFlush(dataPacket);

        return ResultVO.ok();
    }


}
